// index.js
// 获取应用实例
import {
  BaseModel
} from '../../model/base.js'
let report = new BaseModel();
Page({
  data: {
    PI: 3.14159265358979324,
    latitude: '',
    longitude: '',
    markers: [],
    mapWidth: '',
    mapHeight: ''

  },
  toaddress: function (e) {
    console.log(e)
    var id = e.markerId
    console.log(id)
    // wx.openLocation({
    //   latitude: this.data.markers[id].latitude,
    //   longitude: this.data.markers[id].longitude,
    // })
    wx.navigateTo({
      url: '/pages/index/index',
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow: function (options) {
    let that = this;
    wx.getLocation({
      type:'gcj02',
      altitude: 'altitude',
      success: (res) => {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        report.restrooms(res.latitude, res.longitude).then(res => {
          let list = res.data;
          for (let i = 0; i < list.length; i++) {
            let data = list[i];
            let iconPath = '/images/icon.png';
            let callout = {
              content: data.name,
              padding: 10,
              display: 'ALWAYS',
              textAlign: 'center',
            
              color:'#000',

              // borderRadius: 10,
              borderColor:'#000',
              // borderWidth: 2,
            }
            // let label={
            //   padding: 10,
            //   display: 'ALWAYS',
            //   textAlign: 'center',
            
            //   color:'#000',
            // }
            list[i].callout = callout
            // list[i].label = label
            list[i].iconPath = iconPath
            list[i].level = that.getLevel(list[i].locations.female.h2s,list[i].locations.female.ammonia)
            
            list[i].width = "64rpx"
             list[i].height = "64rpx"
            let diss = that.distance(that.data.latitude, that.data.longitude, Number(data.latitude), Number(data.longitude))
            if (diss < 1000) {
              list[i].diss = diss + 'm'
            } else {
              list[i].diss = (diss / 1000) + 'km'
            }
            that.setData({
              markers: list
            })
          }
        })


      }
    })

  },
  markertap:function(e){
    let markerId=e.detail.markerId
    let data=this.data.markers[markerId-1];
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: ""
    })
  },
  goTo: function (e) {
    let data = e.currentTarget.dataset.item;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: ""
    })
  },
  distance: function (latA, lngA, latB, lngB) {
    var earthR = 6371000;
    var x = Math.cos((latA * this.data.PI) / 180) * Math.cos((latB * this.data.PI) / 180) * Math.cos(((lngA - lngB) * this.data.PI) / 180);
    var y = Math.sin((latA * this.data.PI) / 180) * Math.sin((latB * this.data.PI) / 180);
    var s = x + y;
    if (s > 1) s = 1;
    if (s < -1) s = -1;
    var alpha = Math.acos(s);
    var distance = alpha * earthR;
    return distance.toFixed(0);
  },
  getLevel:function (h2s,ammonia) {
    if(h2s<0.5&&ammonia<=0.5){
      return '优'
    }
    if(h2s>1||ammonia>1){
      return '差'
    }
    if((h2s>=0.5&&h2s<=1)||(ammonia>=0.5&&ammonia<=1)){
      return '良'
    }
    return '未知';
  },
  oneTap(e){
    let data = e.currentTarget.dataset.item;
    let current = e.currentTarget.dataset.index;
    this.setData({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      current
    })
  },
  dwTap(){
    let that = this;
    wx.getLocation({
      type:'gcj02',
      altitude: 'altitude',
      success: (res) => {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        report.restrooms(res.latitude, res.longitude).then(res => {
          let list = res.data;
          for (let i = 0; i < list.length; i++) {
            let data = list[i];
            let iconPath = '/images/icon.png';
            let callout = {
              content: data.name,
              padding: 10,
              display: 'ALWAYS',
              textAlign: 'center',
            
              color:'#000',

              // borderRadius: 10,
              borderColor:'#000',
              // borderWidth: 2,
            }
            // let label={
            //   padding: 10,
            //   display: 'ALWAYS',
            //   textAlign: 'center',
            
            //   color:'#000',
            // }
            list[i].callout = callout
            // list[i].label = label
            list[i].iconPath = iconPath
            list[i].level = that.getLevel(list[i].locations.female.h2s,list[i].locations.female.ammonia)
            
            list[i].width = "64rpx"
             list[i].height = "64rpx"
            let diss = that.distance(that.data.latitude, that.data.longitude, Number(data.latitude), Number(data.longitude))
            if (diss < 1000) {
              list[i].diss = diss + 'm'
            } else {
              list[i].diss = (diss / 1000) + 'km'
            }
            that.setData({
              markers: list,
              current:-1
            })
          }
        })


      }
    })
  }
})